package org.hongzhisoft.dis.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hongzhisoft.dis.bean.User;
import org.hongzhisoft.dis.service.UserService;
import org.hongzhisoft.dis.service.impl.UserServiceImpl;

/**
 * 用户修改操作处理类
 * @author HongChengzhi
 *
 */
@WebServlet("/user/update")
public class UserUpdateServlet extends HttpServlet {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        int id = Integer.parseInt(req.getParameter("id"));
        
        try {
            User user = userService.selectById(id);
            req.setAttribute("user", user);
            req.getRequestDispatcher("/userUpdate.jsp").forward(req, resp);
        } catch (Exception e) {
            throw new ServletException(e);
        }
        
    }
    
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        int id = Integer.parseInt(req.getParameter("id"));
        String account = (String) req.getParameter("account");
        String password = (String) req.getParameter("password");
        String remark = (String) req.getParameter("remark");
        
        User user = new User();
        user.setId(id);
        user.setAccount(account);
        user.setPassword(password);
        user.setRemark(remark);

        try {
            userService.updateUser(user);
            req.setAttribute("account", account);
            req.getRequestDispatcher("/update_success.jsp").forward(req, resp);
        } catch (Exception e) {
            throw new ServletException(e);
        }

    }

}
